global rank; global masterRank; global totalNumberOfNodes; global Extra; global dreamPar;

Extra.ModelHome = getenv('MODEL_HOME');
Extra.DREAMHome = pwd;
Extra.ToolsHome = strcat(Extra.DREAMHome,'/','tools');

path(path,Extra.ModelHome);
path(path,Extra.DREAMHome);
path(path,Extra.ToolsHome);

rank = -1;
masterRank = 0;

%user specific settings
eval(strcat('cd ', Extra.ModelHome));
initialize;
eval(strcat('cd ', Extra.DREAMHome));

%initialize DREAM parameters
initDreamPar;

%initialize MPI environment
totalNumberOfNodes = dreamPar.nSeq+1;
% disp(totalNumberOfNodes);
try
    [rank] = initMPIEnvironment(totalNumberOfNodes);
    if rank == masterRank    
        dreamMaster(dreamPar);
    else
        dreamSlave(dreamPar);
    end

    disp(strcat('The process ',num2str(rank), ' finalized.'));
    MPI_Finalize;
catch
    rethrow(lasterror); 
end
